﻿Imports System
Imports System.Collections.ObjectModel
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Runtime
    <Serializable> _
    Public NotInheritable Class CorrelationTokenCollection
        Inherits KeyedCollection([Of] String, CorrelationToken)
        ' Methods
        Protected Overrides Sub ClearItems()
            MyBase.ClearItems
        End Sub

        Public Shared Function GetCorrelationToken(ByVal activity As Activity, ByVal correlationTokenName As String, ByVal ownerActivityName As String) As CorrelationToken
            If (correlationTokenName Is Nothing) Then
                Throw New ArgumentNullException("correlationTokenName")
            End If
            If (ownerActivityName Is Nothing) Then
                Throw New ArgumentNullException("ownerActivityName")
            End If
            If (activity Is Nothing) Then
                Throw New ArgumentNullException("activity")
            End If
            Dim activity2 As Activity = ContextActivityUtils.ContextActivity(activity)
            Dim activityByName As Activity = Nothing
            If Not String.IsNullOrEmpty(ownerActivityName) Then
                Do While (Not activity2 Is Nothing)
                    activityByName = activity2.GetActivityByName(ownerActivityName, True)
                    If (Not activityByName Is Nothing) Then
                        Exit Do
                    End If
                    activity2 = ContextActivityUtils.ParentContextActivity(activity2)
                Loop
                If (activityByName Is Nothing) Then
                    activityByName = Helpers.ParseActivityForBind(activity, ownerActivityName)
                End If
            End If
            If (activityByName Is Nothing) Then
                Throw New InvalidOperationException(ExecutionStringManager.OwnerActivityMissing)
            End If
            Dim tokens As CorrelationTokenCollection = [TryCast](activityByName.GetValue(CorrelationTokenCollection.CorrelationTokenCollectionProperty),CorrelationTokenCollection)
            If (tokens Is Nothing) Then
                tokens = New CorrelationTokenCollection
                activityByName.SetValue(CorrelationTokenCollection.CorrelationTokenCollectionProperty, tokens)
            End If
            If Not tokens.Contains(correlationTokenName) Then
                tokens.Add(New CorrelationToken(correlationTokenName))
            End If
            Return tokens.Item(correlationTokenName)
        End Function

        Public Function GetItem(ByVal key As String) As CorrelationToken
            Return MyBase.Item(key)
        End Function

        Protected Overrides Function GetKeyForItem(ByVal item As CorrelationToken) As String
            Return item.Name
        End Function

        Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As CorrelationToken)
            If (item Is Nothing) Then
                Throw New ArgumentNullException("item")
            End If
            MyBase.InsertItem(index, item)
        End Sub

        Protected Overrides Sub RemoveItem(ByVal index As Integer)
            MyBase.RemoveItem(index)
        End Sub

        Protected Overrides Sub SetItem(ByVal index As Integer, ByVal item As CorrelationToken)
            If (item Is Nothing) Then
                Throw New ArgumentNullException("item")
            End If
            MyBase.SetItem(index, item)
        End Sub

        Friend Shared Sub UninitializeCorrelationTokens(ByVal activity As Activity)
            Dim tokens As CorrelationTokenCollection = [TryCast](activity.GetValue(CorrelationTokenCollection.CorrelationTokenCollectionProperty),CorrelationTokenCollection)
            If (Not tokens Is Nothing) Then
                Dim token As CorrelationToken
                For Each token In tokens
                    token.Uninitialize(activity)
                Next
            End If
        End Sub


        ' Fields
        Public Shared ReadOnly CorrelationTokenCollectionProperty As DependencyProperty = DependencyProperty.RegisterAttached("CorrelationTokenCollection", GetType(CorrelationTokenCollection), GetType(CorrelationTokenCollection))
    End Class
End Namespace

